package com.cug.campus_sh_market_server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cug.campus_sh_market_server.entity.myenum.GoodsStatusEnum;
import com.cug.campus_sh_market_server.entity.myenum.TradingTypeEnum;
import com.cug.campus_sh_market_server.entity.myenum.ViewpointEnum;
import com.cug.campus_sh_market_server.entity.param.OrderParam;
import com.cug.campus_sh_market_server.entity.param.PageParam;
import com.cug.campus_sh_market_server.entity.param.goodstrading.GoodsTradingAddParam;
import com.cug.campus_sh_market_server.entity.param.goodstrading.GoodsTradingQueryParam;
import com.cug.campus_sh_market_server.entity.param.goodstrading.GoodsTradingUpdateParam;
import com.cug.campus_sh_market_server.entity.po.Goods;
import com.cug.campus_sh_market_server.entity.po.GoodsTrading;
import com.cug.campus_sh_market_server.entity.vo.goodstrading.GoodsTradingVO;
import com.cug.campus_sh_market_server.mapper.GoodsMapper;
import com.cug.campus_sh_market_server.service.GoodsTradingService;
import com.cug.campus_sh_market_server.util.PageUtil;
import com.cug.campus_sh_market_server.util.UserContext;
import com.nycreativity.elecreativity.entity.vo.PageDataVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 用户商品购买请求 服务实现类
 * </p>
 *
 * @author sxt
 * @since 2022-05-25
 */
@Service
public class GoodsTradingServiceImpl extends CustomRestfullServiceImpl<GoodsTrading, GoodsTradingQueryParam, GoodsTradingAddParam, GoodsTradingUpdateParam, GoodsTradingVO> implements GoodsTradingService {

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public PageDataVO<GoodsTradingVO> queryDataByPage(GoodsTradingQueryParam goodsTradingQueryParam, PageParam pageParam, OrderParam orderParam) {
        QueryWrapper<GoodsTradingVO> queryWrapper = getQueryWrapper(goodsTradingQueryParam, orderParam);
        if (queryWrapper != null) {
            if (!checkAdminUser()) {
                if (goodsTradingQueryParam.getViewpoint() == ViewpointEnum.SELLER_VIEWPOINT.getTypeCode()) {
                    queryWrapper.lambda().eq(GoodsTradingVO::getGoodsPublisherId, UserContext.get().getId());
                } else if (goodsTradingQueryParam.getViewpoint() == ViewpointEnum.BUYER_VIEWPOINT.getTypeCode()) {
                    queryWrapper.lambda().eq(GoodsTradingVO::getCreator, UserContext.get().getId());
                }
            }
        }
        Page<GoodsTradingVO> selectPage = voBaseMapper.selectPage(new Page<>(pageParam.getCurrentPage(), pageParam.getPageSize()), queryWrapper);
        return PageUtil.convert(selectPage);
    }

    @Transactional
    @Override
    public boolean updateData(GoodsTradingUpdateParam goodsTradingUpdateParam) {
        boolean rs = super.updateData(goodsTradingUpdateParam);
        if (rs) {
            Integer status = goodsTradingUpdateParam.getStatus();
            if (TradingTypeEnum.TRADING_SUCCEED.getTypeCode().equals(status)) {
                GoodsTrading goodsTrading = getById(goodsTradingUpdateParam.getId());
                update(new GoodsTrading().setStatus(TradingTypeEnum.TRADING_REFUSED.getTypeCode()),
                        new LambdaQueryWrapper<GoodsTrading>()
                                .eq(GoodsTrading::getGoodsId, goodsTrading.getGoodsId())
                                .eq(GoodsTrading::getCreator, goodsTrading.getCreator())
                                .ne(GoodsTrading::getId, goodsTrading.getId()));
                Goods goods = goodsMapper.selectById(goodsTrading.getGoodsId());
                if (goods != null) {
                    goods.setStatus(GoodsStatusEnum.SALE_OUT.getTypeCode());
                    goodsMapper.updateById(goods);
                }
            }
        }
        return rs;
    }
}
